package xiao.ke.sbdemo.model;/**
 * @author XuXiaoK
 * @create 2019-11-26 13:11
 */

import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.logging.Logger;

/**
 * @Author XiaoKe
 * @Date 2019/11/26 13:11
 * @Description 全局捕获异常
 */
@RestControllerAdvice
public class ExcHandler {

    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(ExcHandler.class);

    @ExceptionHandler(value = Exception.class)
    Object handlerException(Exception e, HttpServletRequest request) {
        LOGGER.error("url {}, msg{}", request.getRequestURL(), e.getMessage());
        HashMap<String, Object> map = new HashMap<>();
        map.put("code", 100);
        map.put("msg", e.getMessage());
        map.put("url", request.getRequestURL());
        return map;
    }

    @ExceptionHandler(value = MyException.class)
    Object handlerMyException(Exception e) {

        ModelAndView andView = new ModelAndView();
        andView.setViewName("error.html");
        andView.addObject("msg", e.getMessage());
        return andView;
    }
}
